// Copyright Epic Games, Inc. All Rights Reserved.

#include "InputConfigureButton.h"
#include "InputConfigureButtonStyle.h"
#include "InputConfigureButtonCommands.h"
#include "LevelEditor.h"
#include "Widgets/Docking/SDockTab.h"
#include "Widgets/Layout/SBox.h"
#include "Widgets/Text/STextBlock.h"
#include "SWidget/SConfigureWindow.h"
#include "ToolMenus.h"


static const FName InputConfigureButtonTabName("InputConfigureButton");

#define LOCTEXT_NAMESPACE "FInputConfigureButtonModule"

void FInputConfigureButtonModule::StartupModule()
{
	// This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module
	
	FInputConfigureButtonStyle::Initialize();
	FInputConfigureButtonStyle::ReloadTextures();

	FInputConfigureButtonCommands::Register();
	
	PluginCommands = MakeShareable(new FUICommandList);

	PluginCommands->MapAction(
		FInputConfigureButtonCommands::Get().OpenPluginWindow,
		FExecuteAction::CreateRaw(this, &FInputConfigureButtonModule::PluginButtonClicked),
		FCanExecuteAction());

	UToolMenus::RegisterStartupCallback(FSimpleMulticastDelegate::FDelegate::CreateRaw(this, &FInputConfigureButtonModule::RegisterMenus));
	
	FGlobalTabmanager::Get()->RegisterNomadTabSpawner(InputConfigureButtonTabName, FOnSpawnTab::CreateRaw(this, &FInputConfigureButtonModule::OnSpawnPluginTab))
		.SetDisplayName(LOCTEXT("FInputConfigureButtonTabTitle", "InputConfigureButton"))
		.SetMenuType(ETabSpawnerMenuType::Hidden);
}

void FInputConfigureButtonModule::ShutdownModule()
{
	// This function may be called during shutdown to clean up your module.  For modules that support dynamic reloading,
	// we call this function before unloading the module.

	UToolMenus::UnRegisterStartupCallback(this);

	UToolMenus::UnregisterOwner(this);

	FInputConfigureButtonStyle::Shutdown();

	FInputConfigureButtonCommands::Unregister();

	FGlobalTabmanager::Get()->UnregisterNomadTabSpawner(InputConfigureButtonTabName);
}

TSharedRef<SDockTab> FInputConfigureButtonModule::OnSpawnPluginTab(const FSpawnTabArgs& SpawnTabArgs)
{

	return SNew(SDockTab)
		.TabRole(ETabRole::NomadTab)
		.Label(LOCTEXT("InputConfigureTab", "Input Configure"))
		.ToolTipText(LOCTEXT("InputConfigureTabTextToolTip", "Configure UI input"))
		//.OnTabClosed(SDockTab::FOnTabClosedCallback::CreateRaw(this, &FInputConfigureButtonModule::OnTabClosed))
		.Clipping(EWidgetClipping::ClipToBounds)
		[
			SNew(SConfigureWindow)
			//SNew(SButton)
			//.Text(LOCTEXT("Left", "TEST"))
		];
}

void FInputConfigureButtonModule::PluginButtonClicked()
{
	FGlobalTabmanager::Get()->TryInvokeTab(InputConfigureButtonTabName);
}

void FInputConfigureButtonModule::RegisterMenus()
{
	// Owner will be used for cleanup in call to UToolMenus::UnregisterOwner
	FToolMenuOwnerScoped OwnerScoped(this);

	{
		UToolMenu* Menu = UToolMenus::Get()->ExtendMenu("LevelEditor.MainMenu.Window");
		{
			FToolMenuSection& Section = Menu->FindOrAddSection("WindowLayout");
			Section.AddMenuEntryWithCommandList(FInputConfigureButtonCommands::Get().OpenPluginWindow, PluginCommands);
		}
	}

	{
		UToolMenu* ToolbarMenu = UToolMenus::Get()->ExtendMenu("LevelEditor.LevelEditorToolBar");
		{
			FToolMenuSection& Section = ToolbarMenu->FindOrAddSection("Settings");
			{
				FToolMenuEntry& Entry = Section.AddEntry(FToolMenuEntry::InitToolBarButton(FInputConfigureButtonCommands::Get().OpenPluginWindow));
				Entry.SetCommandList(PluginCommands);
			}
		}
	}
}



#undef LOCTEXT_NAMESPACE
	
IMPLEMENT_MODULE(FInputConfigureButtonModule, InputConfigureButton)